// pages/my/orders/index.js
const app = getApp()
const {
  baseURL
} = require('../../../fetch/http')
const api = require('../../../fetch/api')
import Dialog from '../../../miniprogram_npm/@vant/weapp/dialog/dialog';

Page({
  /**
   * 页面的初始数据
   */
  data: {
    baseURL: baseURL,
    tabs: ['全部', '待付款', '待发货', '待收货', '已完成'],
    tabIndex: 0,
    arr: [],
    loading: false,
    finished: false,
    page: 1
  },
  clipBoard() {
    wx.setClipboardData({
      data: this.data.detail.expressNo,
    })
  },
  orderDetail(e) {
    const {
      id,
      type
    } = app.dataSet(e)
    if (type == 1) {
      wx.navigateTo({
        url: '/pages/my/orderDetail/index?id=' + id,
      })
    } else {
      wx.navigateTo({
        url: '/pages/my/integralOrder/index?id=' + id,
      })
    }

  },
  switchTab(e) {
    const {
      index
    } = app.dataSet(e)
    if (this.data.tabIndex != index) {
      this.setData({
        tabIndex: index,
      })
      this.clear()
      this.orderList()
    }
  },
  clear() {
    this.setData({
      arr: [],
      page: 1,
      loading: false,
      finished: false
    })
  },
  async orderList() {
    if (!this.data.loading) {
      this.setData({
        loading: true
      })
      let res = await api.orderList({
        page: this.data.page,
        state: this.data.tabIndex
      })
      let finished = false
      res.data.length == 0 && (finished = true)
      this.setData({
        arr: [...this.data.arr, ...res.data],
        finished,
        loading: false
      })
    }
  },
  // 支付订单
  async pay(e) {
    const {
      id,
      index
    } = app.dataSet(e)
    let res = await api.pay({
      order_id: id
    })
    if (res.code == 200) {
      api.wxpay(res.data).then(res => {
        if (res.errMsg == "requestPayment:ok") {
          app.toast('支付成功', 'success', () => {
            // let item = this.data.arr
            this.setData({
              [`arr[${index}].status`]: 2
            })
          })
        }
      }).catch(error => {
        console.log(error);
      })
    }
  },
  // 确认收货
  async sign(e) {
    const { id, index } = app.dataSet(e)
    let res = await api.sign({
      order_id: id
    })
    if(res.code==200){
      app.toast('签收成功','success',()=>{
        this.clear()
        this.orderList()
      })
    }
  },
  // 取消订单
  async remove(e) {
    const {
      id,
      index
    } = app.dataSet(e)
    Dialog.confirm({
        title: '您确认要取消订单吗？',
        message: '点击确认继续',
      })
      .then(() => {
        // on confirm
        api.clearOrder({
          order_id: id
        }).then(res => {
          if (res.code == 200) {
            let arr = [...this.data.arr]
            arr.splice(index, 1)
            this.setData({
              arr
            })
          }
        })
      })
      .catch(() => {
        // on cancel
      });


  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.index) {
      this.setData({
        tabIndex: options.index
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.clear()
    this.orderList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let {
      page,
      finished
    } = this.data
    if (!finished) {
      this.setData({
        page: page + 1
      })
      this.orderList()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})